package com.customer.service.Impl;

import com.customer.context.BaseContext;
import com.customer.dto.UserLoginDTO;
import com.customer.dto.UserUpdateDTO;
import com.customer.entity.User;
import com.customer.mapper.UserMapper;
import com.customer.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
/**
 * 用户服务实现类
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(UserLoginDTO userLoginDTO) {

        User user = userMapper.getByUsernameAndPassword(userLoginDTO.getUsername(), userLoginDTO.getPassword());
        if (user == null) {
            user = User.builder()
                    .createTime(LocalDateTime.now())
                    .build();
            userMapper.insert(user);
        }
        return user;
    }
    /**
     * 修改用户信息
     *
     * @param userUpdateDTO
     */
    public void update(UserUpdateDTO userUpdateDTO) {
        User user = User.builder()
                .id(userUpdateDTO.getId())
                .nickname(userUpdateDTO.getNickname())
                .avatar(userUpdateDTO.getAvatar())
                .phone(userUpdateDTO.getPhone())
                .updateTime(LocalDateTime.now())
                .build();
        userMapper.update(user);
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    public User getById(Long id) {
        return userMapper.getById(id);
    }
}
